package com.ruicar.afs.cloud.bizcommon.print.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruicar.afs.cloud.bizcommon.dto.print.TemplateDto;
import com.ruicar.afs.cloud.bizcommon.print.entity.ComSealBaseInfo;
import com.ruicar.afs.cloud.bizcommon.print.entity.ComPrintFormSealMapping;
import com.ruicar.afs.cloud.bizcommon.print.enums.TemplateModuleTypeEnum;
import com.ruicar.afs.cloud.bizcommon.print.service.ComPrintFormSealMappingService;
import com.ruicar.afs.cloud.bizcommon.print.service.ComSealBaseInfoService;
import com.ruicar.afs.cloud.bizcommon.print.vo.TemplateSealVo;
import com.ruicar.afs.cloud.bizcommon.print.vo.TemplateSignVo;
import com.ruicar.afs.cloud.common.core.enums.AfsEnumUtil;
import com.ruicar.afs.cloud.common.core.query.QueryCondition;
import com.ruicar.afs.cloud.common.core.util.IResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author rongji.zhang
 * @version 1.0
 * @description: 打印模板印章映射管理
 * @created 2020/8/6
 */

@Slf4j
@RestController
@AllArgsConstructor
@Api(value = "templateSeal", description = "模板印章管理")
@RequestMapping("/templateSeal")
public class ComPrintFormSealMappingController {

    private ComPrintFormSealMappingService comPrintFormSealMappingService;

    private ComSealBaseInfoService comSealBaseInfoService;

    @PostMapping("/querySealMappingList")
    @ApiOperation(value = "查询模板印章列表")
    public IResponse querySealMappingList(@RequestBody QueryCondition<TemplateDto> queryCondition) {
        TemplateDto templateDto=queryCondition.getCondition();
        IPage<ComPrintFormSealMapping> pageResult = comPrintFormSealMappingService.page(new Page(queryCondition.getPageNumber(), queryCondition.getPageSize())
                , Wrappers.<ComPrintFormSealMapping>query().lambda()
                        .eq(StringUtils.isNotEmpty(templateDto.getTemplateId()), ComPrintFormSealMapping::getTemplateId, templateDto.getTemplateId())
                        .eq(ComPrintFormSealMapping::getTemplateModuleType, AfsEnumUtil.key(TemplateModuleTypeEnum.SEAL)));
        return IResponse.success(null != pageResult ? pageResult : "");
    }

    @PostMapping("/querySealList")
    @ApiOperation(value = "查询模板印章列表")
    public IResponse querySealList() {
        List<ComSealBaseInfo> resultList = comSealBaseInfoService.list();
        return IResponse.success(resultList);
    }

    @PostMapping("/addSeal")
    @ApiOperation(value = "添加模板印章映射")
    public IResponse addSeal(@RequestBody ComPrintFormSealMapping comPrintFormSealMapping) {
        try {
            comPrintFormSealMapping.setTemplateModuleType("seal");
            comPrintFormSealMappingService.save(comPrintFormSealMapping);
            return IResponse.success(true);
        }catch (Exception e){
           return IResponse.fail("添加失败");
        }
    }

    @PostMapping("/editSeal")
    @ApiOperation(value = "修改模板印章映射")
    public IResponse editSeal(@RequestBody ComPrintFormSealMapping comPrintFormSealMapping) {
        try {
            comPrintFormSealMappingService.updateById(comPrintFormSealMapping);
            return IResponse.success(true);
        }catch (Exception e){
            return IResponse.fail("修改失败");
        }
    }

    @PostMapping("/delSeal")
    @ApiOperation(value = "删除模板印章映射")
    public IResponse delSeal(@RequestBody ComPrintFormSealMapping comPrintFormSealMapping) {
        try {
            comPrintFormSealMappingService.removeById(comPrintFormSealMapping.getId());
            return IResponse.success(true);
        }catch (Exception e){
            return IResponse.fail("删除失败");
        }
    }

    @PostMapping("/querySignMappingList")
    @ApiOperation(value = "查询模板印章列表")
    public IResponse querySignMappingList(@RequestBody QueryCondition<TemplateDto> queryCondition) {
            TemplateDto templateDto=queryCondition.getCondition();
            IPage<ComPrintFormSealMapping> pageResult = comPrintFormSealMappingService.page(new Page(queryCondition.getPageNumber(), queryCondition.getPageSize())
                    , Wrappers.<ComPrintFormSealMapping>query().lambda()
                            .eq(StringUtils.isNotEmpty(templateDto.getTemplateId()), ComPrintFormSealMapping::getTemplateId, templateDto.getTemplateId())
                            .eq(ComPrintFormSealMapping::getTemplateModuleType, AfsEnumUtil.key(TemplateModuleTypeEnum.SIGN)));
            return IResponse.success(null != pageResult ? pageResult : "");
        }

    @PostMapping("/addSign")
    @ApiOperation(value = "添加模板签名映射")
    public IResponse addSign(@RequestBody ComPrintFormSealMapping comPrintFormSealMapping) {
        try {
            comPrintFormSealMapping.setTemplateModuleType("sign");
            comPrintFormSealMappingService.save(comPrintFormSealMapping);
            return IResponse.success(true);
        }catch (Exception e){
            return IResponse.fail("添加失败");
        }
    }

    @PostMapping("/editSign")
    @ApiOperation(value = "修改模板签名映射")
    public IResponse editSign(@RequestBody ComPrintFormSealMapping comPrintFormSealMapping) {
        try {
            comPrintFormSealMappingService.updateById(comPrintFormSealMapping);
            return IResponse.success(true);
        }catch (Exception e){
            return IResponse.fail("修改失败");
        }
    }

    @PostMapping("/delSign")
    @ApiOperation(value = "删除模板印章映射")
    public IResponse delSign(@RequestBody ComPrintFormSealMapping comPrintFormSealMapping) {
        try {
            comPrintFormSealMappingService.removeById(comPrintFormSealMapping.getId());
            return IResponse.success(true);
        }catch (Exception e){
            return IResponse.fail("删除失败");
        }
    }
}
